package com.systar.activity.report.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;

import com.systar.activity.report.ActivityDataPool;
import com.systar.activity.report.ActivityReportServlet;
import com.systar.activity.report.RequestParameter;
import com.systar.activity.report.users.Role;
import com.systar.activity.report.users.User;

public class GetHtmlData implements ActionProcessor
{
	private static final String revisionAttribute = "revision";

	private Transformer adminTransformer;
	private Transformer regularTransformer;
	
	GetHtmlData()
	{
		// create transformer
		try
		{
			regularTransformer = TransformerFactory.newInstance().newTransformer(
					new javax.xml.transform.stream.StreamSource(
							ActivityReportServlet.class.getResourceAsStream("ActivityStyle.xsl")));
			adminTransformer = TransformerFactory.newInstance().newTransformer(
					new javax.xml.transform.stream.StreamSource(
							ActivityReportServlet.class.getResourceAsStream("ActivityAdminStyle.xsl")));
		}
		catch (TransformerConfigurationException e)
		{
			// skip
			e.printStackTrace();
		}
		catch (TransformerFactoryConfigurationError e)
		{
			// skip
			e.printStackTrace();
		}
	}

	@Override
	public void process(HttpServletRequest request, HttpServletResponse response, Map<RequestParameter, String> parameters, ActivityDataPool data)
			throws ServletException, IOException
	{
		int dataRevision = data.getRevision();
		final Integer sessionRevision;
		final User authenticatedUser;
		synchronized (request.getSession())
		{
			HttpSession session = request.getSession();
			sessionRevision = (Integer)session.getAttribute(revisionAttribute);
			session.setAttribute(revisionAttribute, dataRevision);
			authenticatedUser = (User) session.getAttribute(ActivityReportServlet.authoriseAttribute);
		}
		
		boolean forceDisplay = parameters.containsKey(RequestParameter.force) ? Boolean.parseBoolean(parameters.get(RequestParameter.force)) : false;
		if ((sessionRevision == null) 
				|| (sessionRevision < dataRevision)
				|| forceDisplay)
		{
			System.out.println("Servlet sent html");
			boolean isAdmin = (authenticatedUser != null) ? authenticatedUser.hasRole(Role.admin) : false; 
			Transformer dataTransformer = isAdmin ? adminTransformer : regularTransformer;
			writeHtmlDataFragment(data, dataTransformer, response);
		}
		else
		{
			// nothing change since last get on that session
		}
	}

    private void writeHtmlDataFragment(ActivityDataPool data, Transformer dataTransformer, HttpServletResponse response) throws IOException
    {
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
    	PrintWriter out = response.getWriter();

		data.writeHtmlDataFragment(dataTransformer, out);
    	out.close();
    }
}
